package com.binguo.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.binguo.helper.DateHelper;
import com.binguo.model.api.vo.TecentVO;
import com.binguo.model.mapper.BinguoOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yuritian
 * @Title: TecentYunTuService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020-01-1410:10
 */
@Service
public class TecentYunTuService {

    @Resource
    private BinguoOrderMapper binguoOrderMapper;

    private SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public Integer getDayOrderCount(Integer type){
        String startTime = null;
        String endTime = null;
        if (type == 0) {
            startTime = dateTimeFormat.format(DateHelper.getDayBegin());
            endTime = dateTimeFormat.format(DateHelper.getDayEnd());
        }

        Integer count = binguoOrderMapper.countOrder(startTime,endTime);
        return count;
    }

    public Long getDayOrderSum(Integer type){
        String startTime = null;
        String endTime = null;
        if (type == 0) {
            startTime = dateTimeFormat.format(DateHelper.getDayBegin());
            endTime = dateTimeFormat.format(DateHelper.getDayEnd());
        }
        Long sum = binguoOrderMapper.sumOrder(startTime,endTime);
        return sum;
    }

    public JSONArray getHourSum(){
        Calendar calendar = Calendar.getInstance();
        Integer hourEnd =calendar.get(Calendar.HOUR_OF_DAY);
        Date today = calendar.getTime();
        String date = dateFormat.format(today);
        Integer hourStart = hourEnd - 5;
        String endTime = date + " "+hourEnd+":59:59";
        if (hourStart < 0){
            hourStart = hourStart + 24;
            calendar.add(Calendar.DATE, -1);
            today = calendar.getTime();
            date = dateFormat.format(today);
        }
        String startTime = date + " "+hourStart+":00:00";

        Map<String,Integer> houSumMap = new HashMap<>();
        JSONArray jsonArray = new JSONArray();
        JSONObject tecentHour;
        List<TecentVO> tecentVOList = binguoOrderMapper.selectOrderHourSum(startTime,endTime);
        for (TecentVO tecentVO : tecentVOList){
            houSumMap.put(tecentVO.getX(),tecentVO.getY());
        }
        for (int i = 0 ; i < 6;i++){
            tecentHour = new JSONObject();
            tecentHour.put("x",hourStart+":00");
            if(houSumMap.get(hourStart+"") != null){
                tecentHour.put("y",houSumMap.get(hourStart+""));
            }else {
                tecentHour.put("y",0);
            }
            tecentHour.put("s","s1");
            jsonArray.add(tecentHour);
            hourStart++;
            if (hourStart > 24){
                hourStart = hourStart - 24;
            }
        }

        return jsonArray;
    }


    public JSONArray getSumByDay(){
        JSONArray result = new JSONArray();
        Calendar calendar = Calendar.getInstance();
        Date endDay = calendar.getTime();
        calendar.add(Calendar.DATE, -6);
        Date startDay = calendar.getTime();
        String startTime = dateFormat.format(startDay) + " 00:00:00";
        String endTime = dateFormat.format(endDay) + " 23:59:59";
        List<TecentVO> tecentVOList = binguoOrderMapper.selectOrderDaySum(startTime,endTime);
        JSONObject tecentDay;
        for (TecentVO tecentVO : tecentVOList){
            tecentDay = new JSONObject();
            tecentDay.put("x",tecentVO.getX().substring(5));
            tecentDay.put("y",tecentVO.getY());
            result.add(tecentDay);
        }
        return result;
    }

    public JSONArray getSourceCount(){
        String startTime = dateTimeFormat.format(DateHelper.getDayBegin());
        String endTime = dateTimeFormat.format(DateHelper.getDayEnd());

        List<TecentVO> tecentVOList = binguoOrderMapper.selectCountBySource(startTime,endTime);

        JSONArray result = new JSONArray();
        JSONObject countSource;

        for (TecentVO tecentVO:tecentVOList){
            countSource = new JSONObject();
            countSource.put("y",tecentVO.getY());
            if (tecentVO.getX().equals("1")){
                countSource.put("x","人脸支付");
            }else if (tecentVO.getX().equals("2")){
                countSource.put("x","实体卡支付");
            }else if (tecentVO.getX().equals("3")){
                countSource.put("x","其他支付");
            }else if (tecentVO.getX().equals("4")){
                countSource.put("x","被扫支付");
            }else if (tecentVO.getX().equals("0")){
                countSource.put("x","二维码支付");
            }
            result.add(countSource);
        }
        return result;
    }

}
